/*
 * ClockDelay.cpp
 *
 *  Created on: 2016年10月21日
 *      Author: zhengboyuan
 */

#include "ClockDelay.h"

extern "C"
{
#include "libavutil/time.h"
}


namespace util
{

ClockDelay::ClockDelay():
		m_rate(),
		m_clock(),
		m_pts()
{
}

ClockDelay::~ClockDelay()
{
}

void ClockDelay::setClockRate(int64_t rate)
{
	if (rate > 0)
	{
		m_rate = rate;
	}
}

int64_t ClockDelay::getDelay(int64_t pts)
{
	int64_t delay = 0;
	if (m_clock == 0)
	{
		m_clock = clock();
		m_pts = pts;
		delay = 0;
	}
	else
	{
		delay = (pts - m_pts) * 1000 / m_rate - (clock() - m_clock);
	}

	return delay;
}

bool ClockDelay::getBeginTime(int64_t& clock, int64_t& pts)
{
	clock = m_clock;
	pts = m_pts;

	return true;
}

void ClockDelay::reset()
{
	m_clock = 0;
	m_pts = 0;
}

int64_t ClockDelay::getDuration() const
{
	return clock() - m_clock;
}

int64_t ClockDelay::clock()
{
	return av_gettime_relative() / 1000;
}



} /* namespace util */
